package com.asset.dispoal.flawPool.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.flawPool.domain.db.entity.FlawPoolDo;
import com.asset.dispoal.flawPool.domain.db.mapper.FlawPoolMapper;
import com.asset.dispoal.flawPool.dto.FlawPoolRequest;
import com.asset.dispoal.flawPool.dto.FlawPoolResponse;
import com.asset.dispoal.flawPool.service.converter.FlawPoolConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/5/23$
 */
@Component
public class QueryFlawPoolCommand implements BaseCommand<FlawPoolRequest, Result> {

    @Autowired
    FlawPoolMapper flawPoolMapper;

    @Override
    public Result<PageVO<FlawPoolResponse>> execute(FlawPoolRequest request) {
        FlawPoolDo flawPoolDo = FlawPoolConverter.INSTANCE.reqToDo(request);
        flawPoolDo.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = flawPoolMapper.queryCount(flawPoolDo);
        Page<FlawPoolDo> page = PagingQuery.getPage(count, flawPoolDo);
        PageVO<FlawPoolResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<FlawPoolResponse> flawPoolResponses = flawPoolMapper.queryPage(page);
            pageVO.setResult(flawPoolResponses);
        }
        return Result.ok(pageVO);
    }
}
